#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAX 512

int main(int argc, char * argv[]) {
    int listen_sock = socket(PF_INET, SOCK_STREAM, 0);
    if (argc != 2) {
        printf("Usage : %s <port>\n", argv[0]);
        exit(1);
    }
    struct sockaddr_in addr, client_addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(atoi(argv[1]));

    int rs = bind(listen_sock, (struct sockaddr*)&addr, sizeof addr);
    assert(rs != -1);

    rs = listen(listen_sock, 5);
    assert(rs != -1);

    socklen_t sz = sizeof(client_addr);
    int connfd = accept(listen_sock, (struct sockaddr *)&client_addr, &sz);
    assert(connfd != -1);

    char buf[MAX];
    int len;
    while (len = recv(connfd, buf, sizeof buf, 0)) 
        send(connfd, buf, len, 0);

    close(listen_sock);
    return 0;
}